import fs from 'fs/promises';
import path from 'path';
import yaml from 'yaml';
import { buildSeriesSource } from './series/buildSeriesSource.mjs';
import { buildSocialMediaSources } from './social-media/build-social-media.mjs';
import merge from 'lodash.merge';
/**
 *
 * @param {string} sourceDir Assumed to exist already
 * @param {string} libDir Assumed to exist already
 */
export const generateDemoSources = async (sourceDir, libDir) => {
	const connectionName = 'series_demo_source';
	const connection = {
		name: connectionName,
		type: 'faker',
		options: { seed: 'evidence-series-demo' }
	};
	await fs.writeFile(
		path.join(sourceDir, 'connection.yaml'),
		`
# This file was automatically generated by @evidence-dev/faker-datasource
# This source is designed to contain examples for many series perumtations
${yaml.stringify(connection)}
`.trim()
	);
	const seriesQueries = await buildSeriesSource(sourceDir, libDir, connectionName);
	const socialMediaQueries = await buildSocialMediaSources(sourceDir, libDir, connectionName);

	await fs.writeFile(
		path.join(libDir, 'faker-data-queries.js'),
		`
import { Query } from '@evidence-dev/sdk/usql';
import {query} from "@evidence-dev/universal-sql/client-duckdb";
export const fakerSeries = ${JSON.stringify(merge(socialMediaQueries, seriesQueries), null, 2)
			.replaceAll(/"(Query.create.+)"/g, '$1')
			.replaceAll('\\"', '"')}`
	);
};
